<?php

	/**
	* @package admin
	* @desc Page for redirecting to deleting or editing a user file from database.
	*/
	session_start();	
	

	if ($_SESSION["role"] != "admin")
	{
		header('Location: ../index.php');
		exit();
	}
	
	// Direct calling check
	if(!isset($_GET['action']) || !isset($_GET['id']))
	{
		die("Do not call this page directly");
	}
	
	/**
	* @desc This include file will load the directory settings for the Smarty Templates
	*/
	require '../includes/smarty.inc';
	$smarty->assign('title', "User");
	$smarty->assign('user_id', $_SESSION["user_id"]);	
	$smarty->assign('last', $_SESSION["lastviewed"]);
	$smarty->assign('firstname', $_SESSION["firstname"]);
	$smarty->assign('role', $_SESSION["role"]);
	$smarty->assign('top5', $_SESSION["top5"]);	
	$smarty->assign('pathdepth', "../");
	$smarty->display('header.tpl');
	
	
	
	//get variables
	$action = $_GET['action'];
	$id = $_GET['id'];
	
	/**
	* @desc This include file connects to the database.
	*/
	require '../includes/connect.inc';
	
	//prepare query
	$query = "SELECT * FROM users WHERE user_id = $id";
	
	//run query
	$result = mysql_query($query, $connection) or die("Could not load user info.");
	
	$row = mysql_fetch_array($result);
	
	$user_id = $row["user_id"];
	$user_fname = $row["firstname"];
	$user_lname = $row["lastname"];
	$user_email = $row["email"];
	$user_role = $row["role"];

	mysql_close($connection);
	
	switch($action)
	{
		case "delete":
			$smarty->assign('user_id', $user_id);
			$smarty->assign('user_fname', $user_fname);
			$smarty->assign('user_lname', $user_lname);
			$smarty->assign('user_email', $user_email);
			$smarty->assign('user_role', $user_role);
			
			$smarty->display('admin/deleteuser.tpl');
			break;
			
		case "edit":
			$smarty->assign('user_id', $user_id);
			$smarty->assign('user_fname', $user_fname);
			$smarty->assign('user_lname', $user_lname);
			$smarty->assign('user_email', $user_email);
			$smarty->assign('user_role', $user_role);
			
			if(isset($_SESSION["errors"]["fname"]))
				$smarty->assign('errorfname', $_SESSION["errors"]["fname"]);
			
			if(isset($_SESSION["errors"]["lname"]))
				$smarty->assign('errorlname', $_SESSION["errors"]["lname"]);
			
			if(isset($_SESSION["errors"]["email"]))
				$smarty->assign('erroremail', $_SESSION["errors"]["email"]);
			
			if ($_GET["result"] == "success")
				$smarty->assign('result', "User updated");
				
			$smarty->display('admin/edituser.tpl');
			break;
	}
	
	unset($_SESSION["errors"]);	
	
	
	
	
	$smarty->assign('pathdepth', "../");
	$smarty->display('footer.tpl');
	
?>